/*
 * Test.cpp
 *
 *  Created on: 2010-6-8
 *      Author: guobc
 */

#include <iostream>
#include <fstream>
#include "HTMLDocument.h"
#include "ASTNode.h"
#include <string>
#include "Tools.h"
using namespace HA;
using namespace std;

void printHelpMessage(string execName) {
    cout << "    --Usage: " << execName << " FileName" << endl;
}

bool checkFileExist(string fileName) {
    ifstream fin(fileName.c_str());
    if (fin.is_open()) {
        return true;
    }
    return false;
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        printHelpMessage(argv[0]);
        exit(-1);
    }
    if (!checkFileExist(argv[1])) {
        cout << "    ERROR: The file " << argv[1] << " does not exist." << endl;
        printHelpMessage(argv[0]);
        exit(-1);
    }

    string fileName = argv[1];
	try {
		ASTDom* document = new HTMLDocument();
		bool success = document->open(fileName);
		if (success) {
			document->parse();
			document->close();
			ASTNode *root = document->getRootNode();
			cout << root->toHTML();
		} else {
			cout << "ERROR: Can't open the HTML document" << endl;
		}

	} catch (exception& e) {
		cout << e.what();
	}
}
